package com.action;

import com.config.WeixinPropertiesConfig;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;

@Controller
public class WeixinIndexAction {

    @Autowired
    private WeixinPropertiesConfig weixinPropertiesConfig;

    @Autowired
    private WxMpService wxMpService;

    @RequestMapping("/weixin_index")
    @ResponseBody
    public String index(String echostr, HttpServletRequest request) throws Exception {
        if (echostr != null) {
            return echostr;
        }

        //String token =  wxMpService.getAccessToken();
        //return token;
        WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
        String action = inMessage.getEvent();
        System.out.println(action);
        switch (action) {
            case "subscribe":
                addMenu();
                String openId = inMessage.getFromUser();
                WxMpUser user = wxMpService.getUserService().userInfo(openId);
                System.out.println(openId);
                System.out.println(user.getNickname());
                System.out.println(user.getHeadImgUrl());

                break;
            case "unsubscribe":

                break;
        }

        return "success";
    }

    private void addMenu() throws WxErrorException, UnsupportedEncodingException {
        WxMenuButton  btnOneMenu = new WxMenuButton();
        btnOneMenu.setName("赛杰商城");
        btnOneMenu.setUrl("http://baidu.com");
        btnOneMenu.setType("view");
        //-----------------------------------------------------
        WxMenuButton  btnTwoMenu = new WxMenuButton();
        btnTwoMenu.setName("网址导航");
        btnTwoMenu.setType("click");
        btnTwoMenu.setKey("btnTwoMenu");

        WxMenuButton  btnTwoMenu1 = new WxMenuButton();
        btnTwoMenu1.setName("百度");
        btnTwoMenu1.setType("view");
        btnTwoMenu1.setUrl("http://baidu.com");

        WxMenuButton  btnTwoMenu2 = new WxMenuButton();
        btnTwoMenu2.setName("QQ");
        btnTwoMenu2.setType("view");
        btnTwoMenu2.setUrl("http://qq.com");


        WxMenuButton  btnTwoMenu3 = new WxMenuButton();
        btnTwoMenu3.setName("淘宝");
        btnTwoMenu3.setType("view");
        btnTwoMenu3.setUrl("http://taobao.com");
        btnTwoMenu.setSubButtons(Arrays.asList(btnTwoMenu1,btnTwoMenu2,btnTwoMenu3));
        //---------------------------------------------------
        WxMenuButton  btnThreeMenu = new WxMenuButton();
        btnThreeMenu.setName("个人中心");
        btnThreeMenu.setType("view");
        String btnThreeMenuUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                +  weixinPropertiesConfig.getAppid()
                + "&redirect_uri="
                + URLEncoder.encode( "http://zzsunjob.vicp.cc/center", "utf-8")
                + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
        btnThreeMenu.setUrl(btnThreeMenuUrl);

        //创建菜单
        WxMenu wxMenu = new WxMenu();
        wxMenu.setButtons(Arrays.asList(btnOneMenu,btnTwoMenu,btnThreeMenu));
        wxMpService.getMenuService().menuCreate(wxMenu);

    }


    @RequestMapping("/center")
    public String center(String code) throws WxErrorException {
           System.out.println("###############");
           String openId = wxMpService.oauth2getAccessToken(code).getOpenId();
           WxMpUser user =  wxMpService.getUserService().userInfo(openId);
          return "/center.html";
    }
}
